package com.yunji.web.storage;

import com.github.pagehelper.page.PageMethod;
import com.yunji.aggregate.model.dto.stock.StorageTicketParam;
import com.yunji.aggregate.model.vo.stock.StorageTicketStatisticsVO;
import com.yunji.aggregate.service.StockAggregateService;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.storage.business.StorageTicketBusiness;
import com.yunji.storage.dto.ExpressDTO;
import com.yunji.storage.dto.StorageTicketDTO;
import com.yunji.storage.vo.StorageTicketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.storage
 * @Title: StorageTicketController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-16 13:59:33      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-出入库管理")
@RestController
@RequestMapping("storage/ticket")
public class StorageTicketController extends BaseController {

    @Resource
    StorageTicketBusiness business;

    @Resource
    StockAggregateService aggregateService;

    @ApiOperation("导出出入库记录")
    @PostMapping("/export")
//    @PreAuthorize("@ss.hasPermi('inventory:warehousing:export')")
    public void export(StorageTicketParam param) {
        List<StorageTicketStatisticsVO> list = aggregateService.searchStorageTicketList(param);
        ExcelUtil<StorageTicketStatisticsVO> util = new ExcelUtil<>(StorageTicketStatisticsVO.class);
        util.exportExcel(response, list, "出入库记录数据");
    }

    /**
     * 查询出入库记录
     *
     * @param param
     * @return
     */
    @ApiOperation("查询出入库记录列表")
    @PostMapping("/list")
    public Response<List<StorageTicketStatisticsVO>> list(@RequestBody @Valid StorageTicketParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<StorageTicketStatisticsVO> list = aggregateService.searchStorageTicketList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * 新增出入库单据
     *
     * @param
     * @return
     */
    @ApiOperation("新增出入库单据")
    @Log(title = "新增出入库单据", businessType = BusinessType.INSERT)
//    @PreAuthorize("@ss.hasPermi('inventory:outbound:add')")
    @PostMapping
    public Response<Long> add(@RequestBody StorageTicketDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    /**
     * 修改出入库单据
     *
     * @param dto
     * @return
     */
    @ApiOperation("出入库操作")
//    @PreAuthorize("@ss.hasPermi('inventory:warehousing:in')")
    @PutMapping
    public Response<String> update(@RequestBody StorageTicketDTO dto) {
        business.update(dto);
        return ResponseHelper.success("修改成功");
    }

    /**
     * 出入库详情
     *
     * @param ticketId
     * @return
     */
    @ApiOperation("出入库详情")
    @GetMapping(value = "/{ticketId}")
    public Response<StorageTicketVO> getInfo(@PathVariable Long ticketId) {
        return ResponseHelper.success(business.select(ticketId));
    }

    /**
     * 删除出入库单据
     *
     * @param
     * @return
     */
    @ApiOperation("删除出入库单据")
//    @PreAuthorize("@ss.hasPermi('inventory:warehousing:remove')")
    @DeleteMapping("/{ticketIds}")
    public Response<String> delete(@PathVariable Long... ticketIds) {
        business.delete(ticketIds);
        return ResponseHelper.success("删除成功");
    }

    /**
     * 填写物流
     *
     * @return
     */
    @ApiOperation("填写物流")
    @PostMapping("/bindExpress")
//    @PreAuthorize("@ss.hasPermi('inventory:outbound:fill')")
    public Response<String> bindExpress(@RequestBody ExpressDTO dto) {
        business.bindExpress(dto);
        return ResponseHelper.success("操作成功");
    }

    /**
     * @param dto
     * @Title upload
     * @Desc 上传附件
     * @Date 2024-09-06 14:06:49.164
     * @Return ResponseString>
     */
    @ApiOperation("上传附件")
    @PostMapping("upload")
//    @PreAuthorize("@ss.hasPermi('inventory:warehousing:upload')")
    public Response<String> upload(@RequestBody StorageTicketDTO dto) {
        business.upload(dto);
        return ResponseHelper.success("操作成功");
    }

    /*------------------------------------------------搜索栏------------------------------------------*/

}